<template>
  <div>
    <h1>用户审核</h1>
    <div>
      <el-button
        :disabled="multipleSelection.length == 0"
        type="primary"
        @click="deleteBatch"
        >批量删除</el-button
      >
    </div>
    <!-- 列表数据 -->
    <div>
      <el-table
        :data="tableData"
        stripe
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="userId" label="用户ID" />
        <el-table-column prop="userName" label="用户名" />
        <el-table-column prop="userPass" label="密码" />
        <el-table-column prop="nickName" label="真实姓名" />
        <el-table-column prop="userMobile" label="手机号码" />
        <el-table-column prop="userEmail" label="邮箱" />
        <el-table-column prop="userAvatar" label="头像">
          <template #default="scope">
            <el-image
              v-if="scope.row.userAvatar != null && scope.row.userAvatar != ''"
              style="width: 50px; height: 50px"
              :src="scope.row.userAvatar"
            />
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="checkStatus" label="审核状态">
          <template #default="scope">
            <el-tag v-if="scope.row.checkStatus == 0" type="primary"
              >待审核</el-tag
            >
            <el-tag v-if="scope.row.checkStatus == 1" type="success"
              >审核通过</el-tag
            >
            <el-tag v-if="scope.row.checkStatus == 2" type="danger"
              >审核驳回</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="checkReason" label="驳回原因" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button
              v-if="scope.row.checkStatus == 0"
              size="small"
              type="primary"
              @click="onCheckShow(scope.row)"
              >审核</el-button
            >
            <el-button
              v-if="scope.row.checkStatus == 0"
              size="small"
              type="danger"
              @click="ondeleteUser(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div>
        <div class="demo-pagination-block">
          <el-pagination
            v-model:current-page="searchForm.pageNum"
            v-model:page-size="searchForm.pageSize"
            :page-sizes="[2, 3, 4, 5]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
      <!-- 用户审核对话框 -->
      <div>
        <el-dialog v-model="dialogFormVisible" title="用户审核" width="500">
          <el-form :model="form">
            <el-form-item label="用户名" :label-width="formLabelWidth">
              <el-input v-model="form.userName" autocomplete="off" disabled />
            </el-form-item>
            <el-form-item label="真实姓名" :label-width="formLabelWidth">
              <el-input v-model="form.nickName" autocomplete="off" disabled />
            </el-form-item>
            <el-form-item label="手机号" :label-width="formLabelWidth">
              <el-input v-model="form.userMobile" autocomplete="off" disabled />
            </el-form-item>
            <el-form-item label="邮箱" :label-width="formLabelWidth">
              <el-input v-model="form.userEmail" autocomplete="off" disabled />
            </el-form-item>
            <el-form-item label="审核" :label-width="formLabelWidth">
              <el-radio-group v-model="form.checkStatus">
                <el-radio value="1">审核通过</el-radio>
                <el-radio value="2">审核驳回</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="驳回原因" :label-width="formLabelWidth">
              <el-input
                :disabled="form.checkStatus == 1"
                v-model="form.checkReason"
                autocomplete="off"
                type="textarea"
              />
            </el-form-item>
          </el-form>
          <template #footer>
            <div class="dialog-footer">
              <el-button @click="dialogFormVisible = false">取消</el-button>
              <el-button type="primary" @click="onSumbitCheck">
                提交
              </el-button>
            </div>
          </template>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from "vue";

import {
  getUserPage,
  checkUser,
  deleteByUserId,
  deleteBatchUser,
} from "../api/api";
import { ElMessage, ElMessageBox } from "element-plus";
import { load } from "@amap/amap-jsapi-loader";

//多选
const multipleSelection = ref<any[]>([]);
const handleSelectionChange = (val: any[]) => {
  multipleSelection.value = val;
  console.log(multipleSelection.value);
};

//批量删除用户
const deleteBatch = () => {
  if (multipleSelection.value.length == 0) {
    ElMessage.warning("请先选择要删除的数据");
    return;
  }
  ElMessageBox.confirm("删除后无法恢复数据，确认要删除吗?", "警告", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      //确认删除
      deleteBatchUser(multipleSelection.value).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("删除成功");
          loadList();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    })
    .catch(() => {
      //取消删除
      ElMessage.error("取消删除");
    });
};

//根据id删除用户
const ondeleteUser = (row: any) => {
  ElMessageBox.confirm("删除后无法恢复数据，确认要删除吗?", "警告", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      //确认删除
      deleteByUserId(row.userId).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("删除成功");
          loadList();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    })
    .catch(() => {
      //取消删除
      ElMessage.error("取消删除");
    });
};

const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const form = reactive({
  userName: "",
  nickName: "",
  userMobile: "",
  userEmail: "",
  userAvatar: "",
  checkStatus: 0,
  checkReason: "",
});
const onCheckShow = (row: any) => {
  Object.assign(form, row);
  dialogFormVisible.value = true;
};
const onSumbitCheck = () => {
  checkUser(form).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("审核完成");
      dialogFormVisible.value = false;
      loadList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

const tableData = ref([]);
const total = ref(0);
const searchForm = ref({
  pageNum: 1,
  pageSize: 2,
});
//分页
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchForm.value.pageSize = val;
  loadList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchForm.value.pageNum = val;
  loadList();
};

const loadList = () => {
  getUserPage(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};

onMounted(() => {
  loadList();
});
</script>

<!-- 分页样式 -->
<style scoped>
.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}
</style>
<!-- 查询表单样式 -->
<style>
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
</style>
